# -*- conding: utf-8 -*-
import sys, pygame
from random import randrange
from pygame.locals import KEYDOWN , QUIT
from itertools import count

size = width, height = 480, 480
up, right, down, left = (0, -10), (10, 0), (0, 10), (-10, 0)

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
snake_dir, food, dead = down, None, False
snake = [pygame.Rect(10, 10 + value * 10, 10, 10) for value in range(10)]
foreground, background = (255, 255, 255), (0, 0, 0)

for counter in count():
    clock.tick(min(5 + (len(snake) / 4), 30))
    event = pygame.event.poll()

    if event.type == QUIT:
        pygame.quit()
        sys.exit()
    

    next = snake[-1].move(snake_dir)
    if next.left < 0: next.left = width - 10
    if next.left >= width: next.left = 0
    if next.top < 0: next.top = height - 10
    if next.top >= height: next.top = 0
    
    if next in snake:
        dead = True
        foreground, background = background, foreground
    else:
        snake.append(next)

        if next == food:
            food = None
        else:
            del snake[0]

    screen.fill(background)
    for rect in snake:
        pygame.draw.rect(screen, foreground, rect)
   

    pygame.display.flip()
